﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class MemberDal
    {
        public MemberDal(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        /// <summary>
        /// 添加会员
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int AddMember(Member m)
        {
            try
            {
                Context.Member.Add(m);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 判断名字是否重复判断手机号是否重复
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public int ChaName(string Name)
        {
            try
            {
                int res = Context.Member.Where(x => x.Name == Name).Count();

                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 判断名字是否重复判断手机号是否重复
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public int ChaPhone(string Phone)
        {
            try
            {
                int res = Context.Member.Where(x => x.Phone == Phone).Count();

                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示查询
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Phone"></param>
        /// <returns></returns>
        public List<Member> ShowMember(string? Name, string? Phone)
        {
            try
            {
                List<Member> list = Context.Member.Where(x => x.IsDeleted == false).ToList();
                if (!string.IsNullOrEmpty(Name))
                {
                    list = Context.Member.Where(x => x.Name.Contains(Name)).ToList();
                }
                if (!string.IsNullOrEmpty(Phone))
                {
                    list = Context.Member.Where(x => x.Phone == Phone).ToList();
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑单删
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteMember(int id)
        {
            try
            {
                var res = Context.Member.Find(id);
                res.IsDeleted = true;
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }

        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Member FanTian(int id)
        {
            try
            {
                return Context.Member.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m">属性</param>
        /// <returns></returns>
        public int UpdateMember(Member m)
        {
            try
            {
                m.UpdateDate = DateTime.Now;
                Context.Member.Update(m);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
